<?php
namespace Pw\MainBundle\Entity;

use Pw\MainBundle\Entity\Hero;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
class Talent
{
	private $id;
	private $heroId;
	private $name;
	private $lvl;
	private $useCost;
	private $learnCost;
	private $rarityId;
	private $coolDown;
	private $description;
	private $parsedDescription;
	private $dateCreated;
	
	private $icon;
	private $iconName;

    /**
     * @var \Pw\MainBundle\Entity\Hero
     */
    private $hero;
    /**
     * @var \Pw\MainBundle\Entity\TalentRarity
     */
    private $rarity;

	public function __construct() {
	}

	public static function loadValidatorMetadata(ClassMetadata $metadata)
    {
		//Icon
		$metadata->addPropertyConstraint('icon', new Assert\Image(array(
			'maxSize' => '1024k',
			'mimeTypesMessage' => 'Please upload a valid image (1MB max)',
        )));
		//Name
		$metadata->addConstraint(new UniqueEntity(array('fields' => 'name', 'groups' => array('add'))));
		$metadata->addPropertyConstraint('name', new Assert\NotBlank());
        $metadata->addPropertyConstraint('name', new Assert\Length(array("max" => 100)));
		//lvl
		$metadata->addPropertyConstraint('lvl', new Assert\NotBlank());
		$metadata->addPropertyConstraint('lvl', new Assert\Type(array('type' => 'integer')));
		$metadata->addPropertyConstraint('lvl', new Assert\Min(array('limit'   => '1')));
		//useCost
		$metadata->addPropertyConstraint('useCost', new Assert\NotBlank());
		//learnCost
		$metadata->addPropertyConstraint('learnCost', new Assert\NotBlank());
		//type
		$metadata->addPropertyConstraint('type', new Assert\NotBlank());
		//coolDown		
		$metadata->addPropertyConstraint('coolDown', new Assert\NotBlank());
	}

	public function getArray4JSON(){
		$res = array();
		$res['id'] = $this->id;
		$res['name'] = $this->name;
		$res['description'] = $this->parsedDescription;
		$res['lvl'] = $this->lvl;
		$res['type'] = $this->type;
		$res['heroId'] = $this->heroId;
                $res['iconName'] = $this->iconName;
		if ($this->rarity) {
			$res['rarity'] = $this->rarity->getArray4JSON();
		}
		if (is_numeric($this->useCost)) {
			$res['useCost'] = $this->useCost;
		}		
		if (is_numeric($this->learnCost)) {
			$res['learnCost'] = $this->learnCost;
		}		
		if (is_numeric($this->coolDown)) {
			$res['coolDown'] = $this->coolDown;
		}		
		return $res;
	}
	
	public function getAbsolutePath($fileName){
        return $this->getUploadRootDir() . '/' . $fileName;
    }
    public function getWebPath($fileName){
        return $this->getUploadDir() . '/' . $fileName;
    }
	public function getDefaultImageSrc(){
		return $this->getWebPath('default.jpg');
	}
	public function getIconSrc(){
		if (!is_file($this->getAbsolutePath($this->iconName))) {
			return $this->getDefaultImageSrc();
		}
		return $this->getWebPath($this->iconName);
	}
    protected function getUploadRootDir(){
        return __DIR__ . '/../../../../web/' . $this->getUploadDir();
    }
    protected function getUploadDir(){
        return 'images/talents';
    }
//////////////////////////////////////////////
	public function doUpload(){
		if ($this->icon) {
			//try to delete existing file
			if ($this->iconName) {
				$this->doDelete($this->iconName);
			}
			$this->iconName = uniqid() . '.' . $this->icon->guessExtension();
			$this->icon->move($this->getUploadRootDir(), $this->iconName);
			unset($this->icon);			
		}
	}
	private function doDelete($fName){
		$file = $this->getAbsolutePath($fName);
		if (is_file($file)) {
			unlink($file);
		}
	}
	public function preRemove(){
		$this->filesToRemove[] = $this->iconName;
	}
	public function postRemove(){
		if (is_array($this->filesToRemove)) {
			foreach ($this->filesToRemove as $fileName) {
				$this->doDelete($fileName);
			}
		}
	}
//////////////////////////////////////////////
	public function getIcon(){
		return $this->icon;
	}
	public function setIcon($icon){
		$this->icon = $icon;
	}
	/**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set heroId
     *
     * @param integer $heroId
     * @return Talent
     */
    public function setHeroId($heroId)
    {
        $this->heroId = $heroId;
    
        return $this;
    }

    /**
     * Get heroId
     *
     * @return integer 
     */
    public function getHeroId()
    {
        return $this->heroId;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Talent
     */
    public function setName($name)
    {
        $this->name = $name;
    
        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set iconName
     *
     * @param string $iconName
     * @return Talent
     */
    public function setIconName($iconName)
    {
        $this->iconName = $iconName;
    
        return $this;
    }

    /**
     * Get iconName
     *
     * @return string 
     */
    public function getIconName()
    {
        return $this->iconName;
    }

    /**
     * Set description
     *
     * @param string $description
     * @return Talent
     */
    public function setDescription($description)
    {
        $this->description = $description;
    
        return $this;
    }

    /**
     * Get description
     *
     * @return string 
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * Set lvl
     *
     * @param integer $lvl
     * @return Talent
     */
    public function setLvl($lvl)
    {
        $this->lvl = $lvl;
    
        return $this;
    }

    /**
     * Get lvl
     *
     * @return integer 
     */
    public function getLvl()
    {
        return $this->lvl;
    }

    /**
     * Set useCost
     *
     * @param integer $useCost
     * @return Talent
     */
    public function setUseCost($useCost)
    {
        $this->useCost = $useCost;
    
        return $this;
    }

    /**
     * Get useCost
     *
     * @return integer 
     */
    public function getUseCost()
    {
        return $this->useCost;
    }

    /**
     * Set learnCost
     *
     * @param integer $learnCost
     * @return Talent
     */
    public function setLearnCost($learnCost)
    {
        $this->learnCost = $learnCost;
    
        return $this;
    }

    /**
     * Get learnCost
     *
     * @return integer 
     */
    public function getLearnCost()
    {
        return $this->learnCost;
    }

    /**
     * Set coolDown
     *
     * @param integer $coolDown
     * @return Talent
     */
    public function setCoolDown($coolDown)
    {
        $this->coolDown = $coolDown;
    
        return $this;
    }

    /**
     * Get coolDown
     *
     * @return integer 
     */
    public function getCoolDown()
    {
        return $this->coolDown;
    }

    /**
     * Set dateCreated
     *
     * @param \DateTime $dateCreated
     * @return Talent
     */
    public function setDateCreated($dateCreated)
    {
        $this->dateCreated = $dateCreated;
    
        return $this;
    }

    /**
     * Get dateCreated
     *
     * @return \DateTime 
     */
    public function getDateCreated()
    {
        return $this->dateCreated;
    }

    /**
     * Set hero
     *
     * @param \Pw\MainBundle\Entity\Hero $hero
     * @return Talent
     */
    public function setHero(Hero $hero = null)
    {
        $this->hero = $hero;
    
        return $this;
    }

    /**
     * Get hero
     *
     * @return \Pw\MainBundle\Entity\Hero 
     */
    public function getHero()
    {
        return $this->hero;
    }

    /**
     * Set desc
     *
     * @param string $desc
     * @return Talent
     */
    public function setParsedDescription($parsedDescription)
    {
        $this->parsedDescription = $parsedDescription;
    
        return $this;
    }

    /**
     * Get desc
     *
     * @return string 
     */
    public function getParsedDescription()
    {
        return $this->parsedDescription;
    }
    /**
     * Set rarityId
     *
     * @param integer $rarityId
     * @return Talent
     */
    public function setRarityId($rarityId)
    {
        $this->rarityId = $rarityId;
    
        return $this;
    }

    /**
     * Get rarityId
     *
     * @return integer 
     */
    public function getRarityId()
    {
        return $this->rarityId;
    }

    /**
     * Set type
     *
     * @param string $type
     * @return Talent
     */
    public function setType($type)
    {
        $this->type = $type;
    
        return $this;
    }

    /**
     * Get type
     *
     * @return string 
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * Set rarity
     *
     * @param \Pw\MainBundle\Entity\TalentRarity $rarity
     * @return Talent
     */
    public function setRarity(\Pw\MainBundle\Entity\TalentRarity $rarity = null)
    {
        $this->rarity = $rarity;
    
        return $this;
    }

    /**
     * Get rarity
     *
     * @return \Pw\MainBundle\Entity\TalentRarity 
     */
    public function getRarity()
    {
        return $this->rarity;
    }
    /**
     * @var string
     */
    private $type;


}